package DAL;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entities.ConnectionManager;
import entities.UserMakesVote;

public class UserMakesVoteDA {
	public UserMakesVote[] selectUserMakesVotes(UserMakesVote user)
	{
		boolean whereClause = false;
		String query = "SELECT * from UserMakesVote  " ;
		if(user.getAdminID() != -1)
		{
			query += "WHERE AdminID = " + user.getAdminID();
			whereClause = true;
		}
				
		if(user.getUserIDVoter() != -1) 
		{
			if(whereClause)
				query += " AND userIDVoter = " + user.getUserIDVoter() + "";
			else
				query += " WHERE userIDVoter = " + user.getUserIDVoter() + "";
			whereClause = true;
		}
		if(user.getVotingID() != -1) 
		{
			if(whereClause)
				query += " AND VotingID = " + user.getVotingID() + "";
			else
				query += " WHERE VotingID = " + user.getVotingID() + "";
			whereClause = true;
		}
		if(!user.getVotingHash().equals("")) 
		{
			if(whereClause)
				query += " AND VotingHash = '" + user.getVotingHash() + "'";
			else
				query += " WHERE VotingHash = '" + user.getVotingHash() + "'";
			whereClause = true;
		}

		ConnectionManager con = new ConnectionManager();
		ResultSet rs = con.executeSelectQuery(query);
		
		ArrayList<UserMakesVote> users = new ArrayList<UserMakesVote>();

		
		try {
			while(rs.next())
			{
				UserMakesVote tempUser= new UserMakesVote();
				tempUser.setUserIDVoter(rs.getInt("UserIDVoter"));
				tempUser.setAdminID(rs.getInt("adminID"));
				tempUser.setVotingID(rs.getInt("VotingID"));
				tempUser.setVotingHash(rs.getString("VotingHash"));
				users.add(tempUser);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		UserMakesVote [] temp = new UserMakesVote [0];
		return users.toArray(temp);

	}


	public boolean updateUserMakesVote(UserMakesVote user)
	{
		boolean setClause = false;
		String query = "UPDATE UserMakesVote SET " ;
		if(user.getAdminID()!= -1) 
		{
			query += " AdminID = " + user.getAdminID() + "  ";
			setClause = true;
		}
		if(user.getUserIDVoter()!= -1) 
		{
			if(setClause)
				query += " , UserIDVoter = " + user.getUserIDVoter() + "";
			else
				query += " UserIDVoter = " + user.getUserIDVoter() + "";
			setClause = true;
		}
		if(user.getVotingID()!= -1) 
		{
			if(setClause)
				query += " , VotingID = " + user.getVotingID() + "";
			else
				query += " VotingID = " + user.getVotingID() + "";
			setClause = true;
		}
		if(!user.getVotingHash().equals("")) 
		{
			if(setClause)
				query += " , VotingHash = '" + user.getVotingHash() + "'";
			else
				query += " VotingHash = '" + user.getVotingHash() + "'";
			setClause = true;
		}
		
		
		query += " WHERE UserIDVoter = "+ user.getUserIDVoter() 
		+ " AND AdminID = " + user.getAdminID()
		+ " AND VotingID = " + user.getVotingID();
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return (x==1);
	}
	
	public boolean insertUserMakesVote(UserMakesVote user)
	{
		String query = "INSERT INTO UserMakesVote (UserIDVoter , AdminID , VotingID,VotingHash ) VALUES " +
				"(" +user.getUserIDVoter() + " , " + user.getAdminID() + " , " + user.getVotingID() + 
				" , '" + user.getVotingHash() +  "')";
		
		ConnectionManager con = new ConnectionManager();

		int x = con.executeUpdateQuery(query);
		
		return x==1;
	}
	
	public int deleteUserMakesVote(UserMakesVote user)
	{
		boolean whereClause = false;
		String query = "DELETE FROM UserMakesVote ";
		if(user.getAdminID() != -1)
		{
			query += "WHERE AdminID = " + user.getAdminID();
			whereClause = true;
		}
				
		if(user.getUserIDVoter() != -1) 
		{
			if(whereClause)
				query += " AND userIDVoter = " + user.getUserIDVoter() + "";
			else
				query += " WHERE userIDVoter = " + user.getUserIDVoter() + "";
			whereClause = true;
		}
		if(user.getVotingID() != -1) 
		{
			if(whereClause)
				query += " AND VotingID = " + user.getVotingID() + "";
			else
				query += " WHERE VotingID = " + user.getVotingID() + "";
			whereClause = true;
		}
		if(!user.getVotingHash().equals("")) 
		{
			if(whereClause)
				query += " AND VotingHash = '" + user.getVotingHash() + "'";
			else
				query += " WHERE VotingHash = '" + user.getVotingHash() + "'";
			whereClause = true;
		}
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x;
	}
	
	
	public static void main(String[] args) {
		UserMakesVote u = new UserMakesVote();

		
		//u.setEmail("asdsadasd");
		
		//u.setAdminID(2);
		//u.setUserIDVoter(5);
		u.setVotingID(2);
		//u.setVotingHash("asdsd111");
		
		UserMakesVoteDA da = new UserMakesVoteDA();
		System.out.println(da.deleteUserMakesVote(u));
	}

}
